package team.nmsg.ge.system.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import team.nmsg.ge.system.util.ServerParams;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Controller   基类  包含了一些基础方法
 * @author Guodeqi 
 * @since 20160802 
 */
public abstract class BaseController {
	
    public final static String ERR_MSG_KEY = "errorMsg";
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
    
	public void writeJson(JSONObject retJson ,  HttpServletResponse httpResp){
		httpResp.setContentType("application/json; charset="+ServerParams.ENCODE);
		httpResp.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		try {
			pw = httpResp.getWriter();
			pw.print(retJson);
			pw.flush();
		} catch (IOException e) {
			logger.error("" , e);
		} finally {
			pw.close();
		}
	}
    
	
	public void writeJson(JSONArray retJson  , HttpServletResponse httpResp ){
		httpResp.setContentType("application/json; charset="+ServerParams.ENCODE);
		httpResp.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = null;
		try {
			pw = httpResp.getWriter();
			pw.print(retJson);
			pw.flush();
		} catch (IOException e) {
			logger.error("" , e);
		} finally {
			pw.close();
		}
	}
	
	/**
	 * 表单 提交的时候 若有文件  则会出现乱码， 故前台传递中文的时候 需要 encodeURI(chineseStr)
	 * 这边拿到后 URLDecoder.decode
	 * @param paraKey
	 * @return
	 */
	public String getParaByConvert(String paraKey , HttpServletRequest httpReq  ){
		
		String parameter = httpReq.getParameter(paraKey);
		if(parameter == null ||parameter.equals("null")){
			 return null;
		}
		String retValue = parameter;
		try {
			retValue = URLDecoder.decode(parameter, "utf-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("" , e);
		}
		
		return retValue;
	}
	
	/**
	 * 获取客户端的 IP
	 * @param request
	 * @return 客户端的 IP
	 */
	public static String getClientIp(HttpServletRequest request) {
	    final String UNKNOWN = "unknown";
	    // 需要注意有多个 Proxy 的情况: X-Forwarded-For: client, proxy1, proxy2
	    // 没有最近的 Proxy 的 IP, 只有 1 个 Proxy 时是客户端的 IP
	    String ip = request.getHeader("X-Forwarded-For");
	    if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
	        ip = request.getHeader("X-Real-IP");
	    }
	    if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
	        ip = request.getHeader("Proxy-Client-IP");
	    }
	    if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
	        ip = request.getHeader("WL-Proxy-Client-IP");
	    }
	    if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
	        ip = request.getHeader("HTTP_CLIENT_IP");
	    }
	    if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
	        ip = request.getHeader("HTTP_X_FORWARDED_FOR");
	    }
	    if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
	        ip = request.getRemoteAddr(); // 没有使用 Proxy 时是客户端的 IP, 使用 Proxy 时是最近的 Proxy 的 IP
	    }
	    return ip;
	}
	
	
}
